package cn.itcast.jdbc;

import java.sql.*;
aaaaa
第二个人写的一行代码
public class JdbcTest {
    public static void main(String[] args) {
        Connection con = null;
        PreparedStatement ps = null;
        ResultSet re = null;

        //注册驱动
        try {
            Class.forName("com.mysql.jdbc.Driver");
            String uri = "jdbc:mysql://mysql_77/mybatis_day01";
            String user = "root";
            String password = "root";
            //获取连接
            String sql = null;
            try {
                con = DriverManager.getConnection(uri, user, password);
                sql = "select *from ta_user where id=?";
            } catch (SQLException e) {
                e.printStackTrace();
            }
            try {
                ps = con.prepareStatement(sql);
                ps.setLong(1, 1);
                re = ps.executeQuery();
                while (re.next()) {
                    System.out.println(re.getString("name"));
                    System.out.println(re.getInt("age"));
                    System.out.println(re.getInt("sex"));
                }
            } catch (SQLException e) {
                e.printStackTrace();
            }
        } catch (ClassNotFoundException e) {
            e.printStackTrace();

        } finally {
            //释放资源
            try {
                if (re != null) {
                    re.close();
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
            try {
                if (ps != null) {
                    ps.close();
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
            try {
                if (con != null) {
                    con.close();
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

    }
}
